import java.util.Scanner;
import java.sql.*;

public class Main {
    private static final Scanner scanner = new Scanner(System.in);
    private static final StudentMgmtService service = new StudentMgmtService();

    public static void main(String[] args) {
        while (true) {
            System.out.println("\n===== 学生信息管理系统 =====");
            System.out.println("1. 登录\n2. 退出");
            System.out.print("请选择：");
            int choice = scanner.nextInt();
            scanner.nextLine();  // 消耗换行符

            if (choice == 1) {
                loginMenu();
            } else if (choice == 2) {
                System.out.println("退出系统");
                break;
            }
        }
    }

    private static void loginMenu() {
        System.out.print("请输入用户名：");
        String username = scanner.nextLine();
        System.out.print("请输入密码：");
        String password = scanner.nextLine();
        System.out.print("请输入角色（student/teacher/admin）：");
        String role = scanner.nextLine();

        try {
            User currentUser = service.login(username, password, role);
            if (currentUser != null) {
                System.out.println("登录成功！");
                switch (currentUser.getRole()) {
                    case "student": studentMenu(currentUser.getUsername()); break;
                    case "teacher": teacherMenu(currentUser); break;
                    case "admin": adminMenu(); break;
                    default: System.out.println("无效角色");
                }
            } else {
                System.out.println("用户名、密码或角色错误");
            }
        } catch (Exception e) {
            System.out.println("登录异常：" + e.getMessage());
        }
    }

    private static void studentMenu(String username) {
        while (true) {
            System.out.println("\n===== 学生菜单 =====");
            System.out.println("1. 查看成绩\n2. 返回登录");
            System.out.print("请选择：");
            int choice = scanner.nextInt();
            scanner.nextLine();

            if (choice == 1) {
                try {
                    service.studentViewScore(username);
                } catch (Exception e) {
                    System.out.println("查询失败：" + e.getMessage());
                }
            } else if (choice == 2) {
                break;
            }
        }
    }

    private static void teacherMenu(User currentUser) {
        while (true) {
            System.out.println("\n===== 教师菜单 =====");
            System.out.println("1. 录入Java成绩\n2. 录入OS成绩\n3. 查看/修改成绩（示例：按学号查询）\n4. 返回登录");
            System.out.print("请选择：");
            int choice = scanner.nextInt();
            scanner.nextLine();

            if (choice == 1) {
                System.out.print("请输入学生学号：");
                String studentId = scanner.nextLine();
                System.out.print("请输入Java成绩：");
                double score = scanner.nextDouble();
                scanner.nextLine();
                try {
                    String teacherCourse = currentUser.getCourse();
                    service.teacherUpdateJavaScore(teacherCourse, studentId, score);
                } catch (Exception e) {
                    System.out.println("录入失败：" + e.getMessage());
                }
            } else if (choice == 2) {
                System.out.print("请输入学生学号：");
                String studentId = scanner.nextLine();
                System.out.print("请输入OS成绩：");
                double score = scanner.nextDouble();
                scanner.nextLine();
                try {
                    String teacherCourse = currentUser.getCourse();
                    service.teacherUpdateOSScore(teacherCourse, studentId, score);
                } catch (Exception e) {
                    System.out.println("录入失败：" + e.getMessage());
                }
            } else if (choice == 3) {
                // 按姓名或学号查询学生
                System.out.print("请输入学生姓名或学号：");
                String keyword = scanner.nextLine();
                try {
                    service.queryStudentByKeyword(keyword);
                    // 询问是否修改成绩
                    System.out.print("是否要修改该学生的成绩？(Y/N): ");
                    String confirm = scanner.nextLine();
                    if ("Y".equalsIgnoreCase(confirm)) {
                        System.out.print("请输入学生学号（确认修改目标）：");
                        String studentId = scanner.nextLine();
                        System.out.println("请选择要修改的课程：");
                        System.out.println("1. Java成绩");
                        System.out.println("2. OS成绩");
                        System.out.print("请选择：");
                        int courseChoice = scanner.nextInt();
                        scanner.nextLine();
                        System.out.print("请输入新成绩：");
                        double score = scanner.nextDouble();
                        scanner.nextLine();
                        String teacherCourse = currentUser.getCourse();
                        if (courseChoice == 1) {
                            service.teacherUpdateJavaScore(teacherCourse, studentId, score);
                        } else if (courseChoice == 2) {
                            service.teacherUpdateOSScore(teacherCourse, studentId, score);
                        } else {
                            System.out.println("无效的课程选择");
                        }
                    }
                } catch (Exception e) {
                    System.out.println("查询失败：" + e.getMessage());
                }
            } else if (choice == 4) {
                break;
            }
        }
    }

    private static void adminMenu() {
        while (true) {
            System.out.println("\n===== 管理员菜单 =====");
            System.out.println("1. 录入学生信息\n2. 查看所有学生信息\n3. 查看所有教师信息\n4. 按学号/姓名查询学生信息\n5. 按姓名查询教师信息\n6. 删除学生信息\n7. 返回登录");
            System.out.print("请选择：");
            int choice = scanner.nextInt();
            scanner.nextLine();

            if (choice == 1) {
                System.out.print("请输入学生用户名：");
                String username = scanner.nextLine();
                System.out.print("请输入学生学号：");
                String studentId = scanner.nextLine();
                System.out.print("请输入学生密码：");
                String password = scanner.nextLine();
                try {
                    service.adminAddStudent(username, studentId, password);
                } catch (Exception e) {
                    System.out.println("录入失败：" + e.getMessage());
                }
            } else if (choice == 2) {
                try {
                    service.adminViewAllStudents();
                } catch (Exception e) {
                    System.out.println("查看失败：" + e.getMessage());
                }
            } else if (choice == 3) {
                try {
                    service.adminViewAllTeachers();
                } catch (Exception e) {
                    System.out.println("查看失败：" + e.getMessage());
                }
            } else if (choice == 4) {
                System.out.print("请输入学生学号或姓名：");
                String keyword = scanner.nextLine();
                try {
                    service.adminQueryStudentByKeyword(keyword);
                } catch (Exception e) {
                    System.out.println("查询失败：" + e.getMessage());
                }
            } else if (choice == 5) {
                System.out.print("请输入教师姓名：");
                String teacherName = scanner.nextLine();
                try {
                    service.adminQueryTeacherByName(teacherName);
                } catch (Exception e) {
                    System.out.println("查询失败：" + e.getMessage());
                }
            } else if (choice == 6) {
                System.out.print("请输入要删除的学生学号或姓名：");
                String keyword = scanner.nextLine();
                try {
                    service.adminDeleteStudent(keyword);
                } catch (Exception e) {
                    System.out.println("删除失败：" + e.getMessage());
                }
            } else if (choice == 7) {
                break;
            }
        }
    }
}